home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9298 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  57 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help : Problems with Arrays of Arrays
  5. Date: 8 Mar 1996 14:09:57 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hqb7lINNg6i@keats.ugrad.cs.ubc.ca>
  8. References: <4houo4$o7@niaomi.iscm.ulst.ac.uk>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4houo4$o7@niaomi.iscm.ulst.ac.uk>,
  12. Shaunna McClintock <shauna@iscm.ulst.ac.uk> wrote:
  13. >
  14. >Hi,
  15. >
  16. >I'm currently writing a program with a structure as follows:
  17. >
  18. >Pop is a structure composing of an int and an array of Chrom
  19. >Chrom is a structure composing of an int and an array of Gene
  20. >Gene is a structure
  21. >
  22. >I've problems in that if in Chrom.. Array of Gene > [3] and Array of 
  23. >Chrom > [30]...the program hangs.. i really need much larger arrays... 
  24. >i'm currently using borlandC on my PC. 
  25.  
  26. Also, consider using dynamic data structures. An array can be replaced by a
  27. pointer to suitable storage that can syntactically act as an array thanks to
  28. the way arrays and pointers behave in C.
  29.  
  30. For example, in chrom, instead of an array, you can declare a pointer to a
  31. a Gene structure. And in the Gene structure, you can declare a pointer to a
  32. Pop structure. You then use the malloc() routine to allocate enough space. For
  33. example, if I declare p to be a pointer to a struct Pop, I can allocate storage
  34. for 50 such contiguous structures and assign it to p, using:
  35.  
  36.     #include <stdlib.h>
  37.  
  38.     /* declaration of struct Pop */
  39.  
  40.     int main(void)
  41.     {
  42.         struct Pop *p = malloc(50 * sizeof(struct Pop));
  43.         
  44.          return 0;
  45.     }
  46.  
  47. For the purpose of accessing the 50 structures, you can treat p as though it
  48. were an array (but keeping in mind that it is not an array, but a pointer to
  49. storage). Thus you could do
  50.  
  51.         p[1].whatever_member = some_value;
  52.  
  53. as you would with an array.
  54.  
  55. -- 
  56.  
  57.